﻿using System.Net.Mail;
using System.Net;
using System.Text;

namespace Apex.Commons.Helper
{
    public class EmailHelper
    {
        private readonly string _smtpServer;
        private readonly int _smtpPort;
        private readonly string _username;
        private readonly string _password;
        private readonly string _fromEmail;
        private readonly string _encoding;

        public EmailHelper(string smtpServer, int smtpPort, string username, string password, string fromEmail, string encoding = "UTF-8")
        {
            _smtpServer = smtpServer;
            _smtpPort = smtpPort;
            _username = username;
            _password = password;
            _fromEmail = fromEmail;
            _encoding = encoding;
        }

        public async Task<bool> SendEmailAsync(string subject, string body, IEnumerable<string> recipients)
        {
            try
            {
                using (var message = new MailMessage())
                {
                    message.From = new MailAddress(_fromEmail);
                    message.Subject = subject;
                    message.Body = body;
                    message.IsBodyHtml = true;
                    message.SubjectEncoding = System.Text.Encoding.GetEncoding(_encoding);
                    message.BodyEncoding = System.Text.Encoding.GetEncoding(_encoding);

                    // 添加多个收件人
                    foreach (var recipient in recipients)
                    {
                        message.To.Add(recipient);
                    }

                    using (var client = new SmtpClient(_smtpServer, _smtpPort))
                    {
                        client.Credentials = new NetworkCredential(_username, _password);
                        client.EnableSsl = false; // 根据配置，不启用SSL
                        client.DeliveryMethod = SmtpDeliveryMethod.Network;

                        await client.SendMailAsync(message);
                        return true;
                    }
                }
            }
            catch (System.Exception ex)
            {
                // 这里可以添加日志记录
                System.Console.WriteLine($"邮件发送失败: {ex.Message}");
                return false;
            }
        }

        // 生成 HTML 表格的方法
        public string GenerateHtmlTable<T>(IEnumerable<T> data, string tableClass = "")
        {
            if (data == null)
                return "<p>没有数据可显示</p>";

            var properties = typeof(T).GetProperties();
            var sb = new StringBuilder();

            sb.Append($"<table class='{tableClass}' border='1' cellspacing='0' cellpadding='5'>");

            // 添加表头
            sb.Append("<tr>");
            foreach (var property in properties)
            {
                sb.Append($"<th>{property.Name}</th>");
            }
            sb.Append("</tr>");

            // 添加表格内容
            foreach (var item in data)
            {
                sb.Append("<tr>");
                foreach (var property in properties)
                {
                    var value = property.GetValue(item) ?? "";
                    sb.Append($"<td>{value}</td>");
                }
                sb.Append("</tr>");
            }

            sb.Append("</table>");
            return sb.ToString();
        }
    }
}
